package com.satis.compose.get.example.pages


import android.graphics.Bitmap
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.satis.compose.get.example.R
import com.satis.compose.get.widget.*
import com.satis.compose.get.annotation.PageRoute
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch


/**
 * @author sunshaobei on 2022/9/9
 */

@Composable
@PageRoute(route = "/app/mvi")
fun TestMVIPage() {
    val viewModel: VM = viewModel()
    val coroutineScope = rememberCoroutineScope()
    val uiState = viewModel.uiState
    val captureController = rememberCaptureController()
    var bitmap by remember {
        mutableStateOf<Bitmap?>(null)
    }
    val rememberScrollState = rememberScrollState()
    GetPage() {
        Scaffold(topBar = { GetTopAppBar(title = { Text(text = "MVI", color = Color.White) }) }) {
            Surface(modifier = Modifier
                .padding(it)) {
                Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                    Column(
                        modifier = Modifier.verticalScroll(state = rememberScrollState),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(text = uiState.text)
                        Button(onClick = {
                            coroutineScope.launch {
                                viewModel.channel.send(Intent.Refresh)
                            }
                        }) {
                            Text(text = "change")
                        }
                        ComposeCapture(captureController = captureController, onSaveBitmap = {
                            bitmap = it
                        }) {
                            Column(
                                horizontalAlignment = Alignment.CenterHorizontally
                            ) {
                                Image(
                                    painter = painterResource(id = R.drawable.wechat),
                                    contentDescription = null
                                )
                                Text(text = "测试截图bitmap")
                                Text(text = "测试截图bitmap")
                                Text(text = "测试截图bitmap")
                                Text(text = "测试截图bitmap")
                                Spacer(modifier = Modifier.height(10.dp))
                            }
                        }
                        Button(onClick = {
                            //触发截图
                            captureController.capture()
                        }) {
                            Text(text = "save")
                        }
                        
                        AnimatedVisibility(visible = bitmap!=null) {
                            Image(bitmap = bitmap!!.asImageBitmap(), contentDescription = null)
                        }
                    }
                }

            }
        }
    }
}


@Preview
@Composable
fun TestMVIScreenPreview() {
    TestMVIPage()
}


class VM : ViewModel() {

    val channel = Channel<Intent>(Channel.UNLIMITED)

    var uiState by mutableStateOf(UIState())

    init {
        viewModelScope.launch {
            channel.consumeAsFlow().collect() {
                dispatch(intent = it)
            }
        }
    }

    var refreshCount = 0
    private fun dispatch(intent: Intent) {
        when (intent) {
            is Intent.Refresh -> {
                viewModelScope.launch {
                    refreshCount++
                    uiState = uiState.copy(text = "refresh $refreshCount")
                }
            }
        }
    }
}

data class UIState(
    val text: String = "default"
)

sealed class Intent() {
    object Refresh : Intent()
}
